package warmachineroster.presentation;

import java.io.IOException;
import java.util.Collections;
import java.util.EventObject;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.gef.ContextMenuProvider;
import org.eclipse.gef.DefaultEditDomain;
import org.eclipse.gef.GraphicalViewer;
import org.eclipse.gef.KeyHandler;
import org.eclipse.gef.KeyStroke;
import org.eclipse.gef.editparts.ScalableRootEditPart;
import org.eclipse.gef.ui.parts.GraphicalEditor;
import org.eclipse.gef.ui.parts.GraphicalViewerKeyHandler;
import org.eclipse.gef.ui.parts.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.actions.ActionFactory;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage;

import warmachineroster.Roster;
import warmachineroster.presentation.dnd.RosterDropTargetListener;
import warmachineroster.presentation.editpart.WarmachineRosterEditpartFactory;

public class WarmachineRosterGraphicalEditor extends GraphicalEditor {

	private final ResourceSet resourceSet = new ResourceSetImpl();

	private Roster roster = null;

	private Resource rosterResource;

	private KeyHandler keyHandler;

	public WarmachineRosterGraphicalEditor() {
		super();
		setEditDomain(new DefaultEditDomain(this));
	}

	@Override
	protected void configureGraphicalViewer() {
		super.configureGraphicalViewer(); 

		GraphicalViewer viewer = getGraphicalViewer();
		viewer.setRootEditPart(new ScalableRootEditPart());
		viewer.setEditPartFactory(new WarmachineRosterEditpartFactory());
		configureKeyhandler(viewer);
		configureContextMenuProvider(viewer);

	}

	protected void configureContextMenuProvider(GraphicalViewer viewer) {
		ContextMenuProvider cmProvider = new WarmachineRosterEditorContextMenuProvider(viewer, getActionRegistry());
		viewer.setContextMenu(cmProvider);
		getSite().registerContextMenu(cmProvider, viewer);
	}

	protected void configureKeyhandler(GraphicalViewer viewer) {
		keyHandler = new GraphicalViewerKeyHandler(getGraphicalViewer());
		viewer.setKeyHandler(keyHandler);
		keyHandler.put(KeyStroke.getPressed(SWT.DEL, 127, 0),
				getActionRegistry().getAction(ActionFactory.DELETE.getId()));
	}

	@Override
	protected void initializeGraphicalViewer() {
		GraphicalViewer viewer = getGraphicalViewer();
		viewer.addDropTargetListener(new RosterDropTargetListener(viewer));
		viewer.setContents(getModel());
	}

	private Roster getModel() {
		return roster;
	}

	@Override
	public void doSave(IProgressMonitor monitor) {
		try {
			rosterResource.save(Collections.EMPTY_MAP);
			getCommandStack().markSaveLocation();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.gef.ui.parts.GraphicalEditor#commandStackChanged(java.util
	 * .EventObject)
	 */
	@Override
	public void commandStackChanged(EventObject event) {
		firePropertyChange(IEditorPart.PROP_DIRTY);
		super.commandStackChanged(event);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.EditorPart#setInput(org.eclipse.ui.IEditorInput)
	 */
	@Override
	protected void setInput(IEditorInput input) {
		super.setInput(input);
		IFile file = ((IFileEditorInput) input).getFile();
		URI uri = URI.createPlatformResourceURI(file.getFullPath().toString(), false);
		URI uriGameSystem = URI.createPlatformPluginURI("/WarmaBuilder/gamesystem/System.wms", false);
		resourceSet.getResource(uriGameSystem, true);
		rosterResource = null;
		try {
			rosterResource = resourceSet.getResource(uri, true);

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		roster = (Roster) rosterResource.getContents().get(0);
		setPartName(file.getName());
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Object getAdapter(Class type) {
		if (type == IContentOutlinePage.class) {
			WarmachineRosterOutlinePage outlinePage = new WarmachineRosterOutlinePage(new TreeViewer());
			outlinePage.setContent(getModel());
			outlinePage.setEditDomain(getEditDomain());
			outlinePage.setActionRegistry(getActionRegistry());
			return outlinePage;
		}
		return super.getAdapter(type);
	}

}
